package com.chauncy.DMS.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import com.chauncy.DMS.bean.ManagerInfoBean;
import com.chauncy.DMS.dao.ManagerDaoImplement;

import util.List2Json1.List2Json1;

public class InputAttInfoServlet extends HttpServlet {

	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {	
			ManagerDaoImplement daoImplement = new ManagerDaoImplement();
			List<ManagerInfoBean> managerList= (List)request.getSession().getAttribute("managerInfoList");
			String buildings = managerList.get(0).getManager_buildings();
			//用于拼接 sql语句
			String mainSql = "SELECT sait.Att_number AS AttId," +
					"sdit.Stu_name AS `name`," +
					"sdit.College AS College," +
					"sdit.Study_major AS major," +
					"sdit.The_Class AS tClass," +
					"sdit.Dorm_number AS dNub," +
					"sait.Is_leave AS qingjia," +
					"sait.Time_for_leave AS qingjiaTime," +
					"sait.Stay_out_late AS wangui," +
					"sait.Late_return_time AS wanguiTime," +
					"sait.No_return AS weigui,sait.remarks AS remarks " +
					"FROM stu_att_info_table AS sait ," +
					"stu_dorm_info_table AS sdit " +
					"WHERE sait.Stu_number =  sdit.Stu_number";
			// limit 当前页显示条目数 page 
			int limit = 0;
			int page = 0;
			String attDate = null;
			String tempDate = request.getParameter("date");
			System.out.println("InputAttInfoServlet:: :: date:"+tempDate);
			try {
				//获取page  limit
				 limit = Integer.parseInt(request.getParameter("limit"));
				 page = Integer.parseInt(request.getParameter("page"));
			} catch (Exception e) {
				// TODO: handle exception
			}
			//当table reload时，(通过日期查询考勤记录时) ajax 将传递过来一个日期 即request.getParameter("date");不为空
			if(tempDate != null && !tempDate.equals("")){
				attDate = "%"+tempDate+"%"; 
			}else{
				attDate ="%"+List2Json1.GetNowDate()+"%" ;
			}
			
			System.out.println("attDate:"+attDate);
			Object[] param = {buildings,attDate,((page-1)*limit),limit};
			//查询当前日期获取需要输入的考勤信息		
			String sql =mainSql + " AND sdit.Dormitory_number = ?  and sait.Att_date like ? ORDER BY dNub ASC limit ?,?";
			String countSql = "select count(*) from ("+ mainSql+" AND sdit.Dormitory_number = ? and sait.Att_date like ? ORDER BY dNub ASC) as mainsql";
			//查询当天需要输入的考勤信息
			List<Map<String, Object>> inputAttInfoList = daoImplement.query(sql, param);
			Object[] param1 = {buildings,attDate};
			JSONObject attInfoJSON = List2Json1.mapToJSON(inputAttInfoList, param1, countSql);
			response.getWriter().print(attInfoJSON);
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
			
	}

}
